package com.gem.back.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.gem.back.entity.AoaScheduleList;
import com.gem.back.entity.AoaStatusList;
import com.gem.back.entity.AoaTypeList;
import com.gem.back.service.IAoaScheduleListService;
import com.gem.back.service.IAoaStatusListService;
import com.gem.back.service.IAoaTypeListService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.SessionAttribute;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * <p>
 *  日程管理前端控制器
 * </p>
 *
 * @author xzhang
 * @since 2020-02-22
 */
@Controller
@RequestMapping("/")
public class AoaScheduleListController {
    @Autowired
    private IAoaScheduleListService aoaScheduleListService;
    @Autowired
    private IAoaTypeListService aoaTypeListService;
    @Autowired
    private IAoaStatusListService aoaStatusListService;

    //日程管理
    @RequestMapping("daymanage")
    public String dayManage(String title,String page, Model model,@SessionAttribute("userId") Integer userId){
        //默认配置
        Integer currentPage = 1;
        Integer pageCount = 10;
        if (title == null){
            title = "";
        }
        if (page != null){
            currentPage = Integer.valueOf(page);
        }
        List<AoaScheduleList> scheduleLists = aoaScheduleListService.findScheduleList(currentPage,pageCount,title,userId);
        int totalPage = aoaScheduleListService.getTotalPage(pageCount,title,userId);
        model.addAttribute("scheduleLists",scheduleLists);
        model.addAttribute("totalPage",totalPage);
        model.addAttribute("currentPage",currentPage);
        model.addAttribute("title",title);
        return "daymanage/daymanage";
    }

    //日程管理修改表单
    @RequestMapping("dayedit")
    public String aboutmeday(Integer rcId,AoaScheduleList scheduleList,Model model){
        QueryWrapper<AoaTypeList> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("type_model","aoa_schedule_list");
        List<AoaTypeList> typeList =  aoaTypeListService.list(queryWrapper);
        QueryWrapper<AoaStatusList> queryWrapper1 = new QueryWrapper<>();
        queryWrapper1.eq("status_model","aoa_schedule_list");
        List<AoaStatusList> statusList = aoaStatusListService.list(queryWrapper1);
        if (rcId != null){
            scheduleList = aoaScheduleListService.getById(rcId);
        }
        model.addAttribute("scheduleList",scheduleList);
        model.addAttribute("typeList",typeList);
        model.addAttribute("statusList",statusList);
        return "daymanage/editday";
    }

    //保存修改
    @RequestMapping("save")
    public String save(AoaScheduleList scheduleList, HttpServletRequest request){
        aoaScheduleListService.saveSchedule(scheduleList,request);
        return "redirect:daymanage";
    }

    //我的日历
    @RequestMapping("daycalendar")
    public String daycalendar() {

        return "daymanage/daycalendar";
    }

}
